define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    
    $.widget('rk.wrCommentCtrl', {
        options: {
            reportId: null,
            isApproved: false //status=1(未阅),2
        },
        commentCon: null,
        _create: function() {
            var me = this;
            var elem = me.element;
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            me._createComment();
        },
        _createComment: function() {
            var me = this;
            var elem = me.element;
            var reportId = me.options.reportId;
            me.commentCon = elem.find('.js-comments');
            //rk.log(me.commentCon.comment)
            me.commentCon.comment({
                open: function(event, ui, a){
                },
                close: function(event, ui){
                },
                previewList: [], //预展示列表
                systemId: workreportService.getSystemId(),
                systemItemId: reportId,
                allusers: [], //若有previewList,需要把对应的user对象放进来
                onCommentSucc: function(e, commentInfo) {
                    me._updateCommentNum(reportId + '');
                },
                onCommentFail: function(e, info) {
                    if (info.status) {}
                },
                onDeleteSucc: function() {
                    me._updateCommentNum(reportId + '');
                }
            });

            var moreBtn = me.commentCon.find('.more_link');
            moreBtn.click(); //display all comments by default

            if (me.options.recipientIsMe && !me.options.isApproved) {
                me._beApproveMode();
            } else {
                me._beCommentMode();
            }
            me.commentCon.find('.publish_button').show();
            // me.commentCon.find('textarea').click().focus(function(e){
            //     e.stopPropagation();//在focus时评论模块会移动滚动条，在此会影响UX
            // });
        },
        commentUrl: null,
        _beApproveMode: function() {
            var me = this;
            var elem = me.element;
            var reportId = me.options.reportId;
            // me.commentCon.find('.green_button').html('批阅');
            // me.commentUrl = me.commentCon.comment( "option", "createurl");//cache
            // me.commentCon.comment( "option", "createurl", '/json/sns_work_report/marking.action');
            // me.commentCon.comment( "option", "postData", {
            //     reportId: reportId
            // });
            // me.commentCon.comment( "option", "onCommentSucc", function(e, commentInfo){
            //     me._beCommentMode();
            //     $('#taskDetailDiv').wrDetailCtrl('showTextOfApproved');
            //     me._updateCommentNum(reportId+'');
            //     if($('#wr-list').size() > 0){
            //         $('#wr-list').wrListCtrl('setApproved', reportId+'');
            //     }
            // });

            //发布前新需求，故改之
            me._createApprovalBtn();
        },
        _createApprovalBtn: function() {
            var me = this;
            var elem = me.element;
            var reportId = me.options.reportId;
            var listId = me.options.listId;

            var commentBtn = me.commentCon.find('.green_button').not('[act="approve"]');
            commentBtn.hide();
            var parent = commentBtn.parent();
            parent.append('<a class="green_button" act="approve" href="#" style="display: block;">'+rk.i18n('WR__APPROVE')+'</a>');
            var approveBtn = parent.find('a[act="approve"]');
            approveBtn.click(function() {
                var url = '/json/sns_work_report/marking.action';
                $.ajax({
                        url: url,
                        data: {
                            reportId: reportId
                        },
                        dataType: 'json',
                        type: 'POST'
                    }).success(function(data) {
                        if (!data || data.status != 0) {
                            return;
                        }
                        approveBtn.remove();
                        commentBtn.show();
                        $('#taskDetailDiv').wrDetailCtrl('showTextOfApproved');
                        //if($('#wr-list').size() > 0){
                        //    $('#wr-list').wrListCtrl('setApproved', reportId+'');
                        //}

                        var body = rk.oa.getPanel().find('.oa-tab-body');
                        var list = body.find('#' + listId + ':visible');
                        if(list.wrListCtrl) list.wrListCtrl('setApproved', reportId + '')

                        me._beCommentMode();
                        if (me.commentCon.find('textarea').val()) {
                            commentBtn.click();
                        }
                        rk.noticeSuccess(rk.i18n('WORKREPORT_IS_READ'));
                    })
                    .error(function() {
                    })
                    .complete(function() {});
                    return false;
            });
        },
        _beCommentMode: function() {
            var me = this;
            var elem = me.element;
            var reportId = me.options.reportId;
            me.commentCon.find('.green_button').html(rk.i18n('APPS_FEED_COMMENT'));
            if (me.commentUrl)
                me.commentCon.comment("option", "createurl", me.commentUrl);
            me.commentCon.comment("option", "postData", null);
            me.commentCon.comment("option", "onCommentSucc", function() {
                me._updateCommentNum(reportId + '');
            });
        },
        _updateCommentNum: function(reportId) {
            var me = this;
            var opt = me.options;
            var num = me._getCommentNum();
            var body = rk.oa.getPanel().find('.oa-tab-body');

            var list = body.find('#' + opt.listId + ':visible');
            if (list.size() > 0) {
                if(list.wrListCtrl) list.wrListCtrl('setCommentNum', reportId, num)
            }
        },
        _getCommentNum: function() {
            var me = this;
            var elem = me.element;
            return elem.find('.show_comment>.comment_item').size();
        }
    });
});
